<?php
require_once( 'Stencil.php' );
 
require( 'util.php' );
validate_action_get( array( 'mid' ) );

$mid = $_GET['mid'];
$page = isset( $_GET['page'] ) ? $_GET['page'] : 1;

require( 'db.php' );
$model = get_model( $mid );

$numThreadsPerPage = 5; // 每页显示多少个帖子
$numPages = 0;

calculate_pages();

?>

<?php

function calculate_pages()
{
    global $mid, $page, $numPages, $numThreadsPerPage;
    $numThreads = 0;
    
    // 只有普通帖子参与页数计算
    $sql = "SELECT COUNT(*) FROM `page` WHERE m_id = $mid AND type = 'O'";
    $result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
    
    if ($result && mysql_num_rows( $result ) )
    {
        $arr = mysql_fetch_array( $result );
        $numThreads = $arr[0];
    }
    
    $numPages = (int) ($numThreads / $numThreadsPerPage);
    if ($numThreads % $numThreadsPerPage)
        $numPages++;
    
    if ($numPages == 0)
        $numPages = 1;
    
    if ($page < 1 || $page > $numPages)
        report_err_and_exit( '无效页码。' );
}

// 页码控制
function output_page_list()
{
    global $mid, $page, $numPages;
    
    echo <<<ROD
<div id="pgt" class="bm bw0 pgs cl" style="margin-top:10px">
    <div class="pg">
ROD;

    for ($i = 1; $i <= $numPages; $i++)
    {
        if ($i == $page)
            echo "<strong>$i</strong>";
        else
            echo "<a href='list.php?mid=$mid&page=$i'>$i</a>";
    }
    
    if ($page < $numPages)
    {
        $next = $page + 1;
        echo "<a href='list.php?mid=$mid&page=$next' class='nxt'>下一页</a>";
    }
    
    echo <<<ROD
    </div>
    <span class="pgb y" id="visitedforums"><a href="#">返回</a></span>
ROD;

    if (can_post_new_page())
    {
        echo <<<ROD
    <a href="new_page.php?mid=$mid&mode=write" id="newspecial" title="发新帖">
        <img src="static/image/common/pn_post.png" alt="发新帖">
    </a>
ROD;
    }
    else
    {
        echo '<div style="height:36px">&nbsp;</div>';
    }
    
    echo '</div>';
}

function output_thread_list($thread_type)
{
    global $mid, $page;

    $sql = "SELECT p_id, user.name as uname, subject, type, dateline, last_replied, comment_times, access_times FROM `page`, `user` WHERE m_id = $mid AND type = '$thread_type' AND user.u_id = page.u_id ORDER BY last_replied DESC ";
	
	if ($thread_type == 'O')
	{
		global $numThreadsPerPage;
		$from = ($page - 1) * $numThreadsPerPage;		
		$sql .= "LIMIT $from, $numThreadsPerPage";
	}
	
    $result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );

    if ($result)
    {
        while ($thread = mysql_fetch_array( $result ))
        {
            output_thread( $thread );
        }
    }
    else
    {
        echo '没有帖子！';
    }
}

function output_thread($thread)
{
    $dateline = date( 'Y-m-d', strtotime( $thread['dateline'] ) );
    $url = 'view_page.php?pid=' . $thread['p_id'];

    echo <<<ROD
<tbody>
    <tr>
ROD;

    switch ($thread['type'])
    {
    case 'N':
    
        echo <<<ROD
        <td class="icn">
            <img src="static/image/common/ann_icon.gif" alt="公告">
        </td>

        <td class="common">
            <span class="xst" style="font-weight:bold;color:red">公告:&nbsp;<a href="$url" class="xst" style="color:red">
ROD;
        break;
        
    case 'T':
    
        echo <<<ROD
        <td class="icn">
            <img src="static/image/common/pin.gif" alt="公告">
        </td>

        <td class="common">
            <span><a href="$url" class="xst" style="color:green">
ROD;
        break;
        
    case 'O':
    
        echo <<<ROD
        <td class="icn">
            <a href="$url" title="新窗口打开" target="_blank"><img src="static/image/common/folder_common.gif" alt="新窗口打开"></a>
        </td>

        <td class="common">
            <span><a href="$url" class="xst">
ROD;
        break;
    }

    echo <<<ROD
        {$thread['subject']}</a></span></td>

        <td class="by">
            <cite>{$thread['uname']}</cite>
            <em><span>$dateline</span></em>
        </td>

        <td class="num">
            <a href="$url" class="xi2">{$thread['comment_times']}</a>
            <em>{$thread['access_times']}</em>
        </td>

        <td class="by">{$thread['last_replied']}</td>
ROD;

    if (is_logged_as_monitor())
	{
		$set_ontop_title = '置顶帖子';
		if ($thread['type'] == 'T')
			$set_ontop_title = '取消' . $set_ontop_title;
		else if ($thread['type'] == 'N')
			$set_ontop_title = '公告转为置顶';
			
		$set_as_notice_title = '将帖子置为公告';
		if ($thread['type'] == 'N')
			$set_as_notice_title = '取消' . $set_as_notice_title;
		else if ($thread['type'] == 'T')
			$set_as_notice_title = '置顶转为公告';
	
        echo <<<ROD
		<td class='mgr'>
			<a href='modify_page_props.php?pid={$thread['p_id']}&action=delete'>
            <img src="static/image/common/del.png" title="删除帖子" /></a>
			<a href='modify_page_props.php?pid={$thread['p_id']}&action=top'>
            <img src="static/image/common/pin.gif" title="$set_ontop_title" /></a>
			<a href='modify_page_props.php?pid={$thread['p_id']}&action=notice'>
            <img src="static/image/common/ann_icon.gif" title="$set_as_notice_title" /></a>
		</td>
ROD;
	}
	else if (is_logged_as_volunteer())
	{
        echo '<td class="join">';
		if ($thread['type'] != 'N')
		{
			echo <<<ROD
			<input id="btnJoin{$thread['p_id']}" name="btnJoin" type="button" value="报名" />
ROD;
		}
		else
		{
			echo '&nbsp;';
		}
		
		echo '</td>';
	}
        
    echo '</tr></tbody>';
}

//********************** 页面内容开始 **********************
?>

<?php

output_frame_header( "帖子列表 - 第 $page 页 - {$model['topic']} - {$model['message']}" );

?>

<link rel="stylesheet" href="static/style/posts.css"/>
<style>
.mgr{width:60px;text-align:center}
.join{width:55px;text-align:center}
.tl .icn {width:30px;padding-right:5px}
</style>

<form name="joinActivityForm" method="post" action="join_activity.php?<?php echo "mid=$mid&page=$page"; ?>">
	<input type="hidden" id="aidVal" name="aid" />
</form>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

	$(function() {
		$("input[name='btnJoin']").click(function() {
			var aid = $(this).attr( "id" ).substr(7); // btnJoin
			$("#aidVal").val( aid );
			document.joinActivityForm.submit();
		});
	});

</script>

<?php output_page_list(); ?>

<div id="threadlist" class="tl bm bmw">
    <div class="th">
        <table cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <th colspan="2"><div class="tf"></div></th>
                    <td class="by">作者</td>
                    <td class="num">回复/查看</td>
                    <td class="by">最后发表</td>
<?php
    if (is_logged_as_monitor())
        echo '<td class="mgr">管理</td>';
	else if (is_logged_as_volunteer())
		echo '<td class="join">报名</td>';
?>
                    
                </tr>
            </tbody>
        </table>
    </div>

    <div class="bm_c">
        <div id="forumnew" style="display:none"></div>
        <table summary="forum_4" cellspacing="0" cellpadding="0">

<?php

if ($page == 1) // 只在第一页显示公告和置顶
{
    output_thread_list( 'N' );
    output_thread_list( 'T' );
}

output_thread_list( 'O' );

?>

        </table>
    </div>
</div>

<?php output_page_list(); ?>

<?php

//********************** 页面内容结束 **********************

output_frame_footer();

?>